<?php
if (! defined('IN_STUCLOUD')) {
    die('访问受限，禁止直接访问文件');
}
require_once dirname(__FILE__) . '/sql/sqlfunctions.php';
require_once dirname(__FILE__) . '/member/member.php';

function hash256($str)
{
    return hash('sha256', $str, false);
}

function getIp()
{
    if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $ip = getenv('REMOTE_ADDR');
    } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches[0] : '';
}

function userLogin($pass, $uid)
{
    return login($pass, $uid);
}

function success($mess)
{
    die(json_encode(array(
        "state" => 1,
        "message" => $mess
    )));
}

function fail($mess)
{
    die(json_encode(array(
        "state" => 0,
        "message" => $mess
    )));
}

function userRegister($name, $pass, $email)
{
    global $_G;
    if ($_G["islogined"]) {
        header("location:index.php");
        return false;
    }
    register($name, $pass, $email);
    header("location:index.php");
}

function userlogout()
{
    sessionClose();
}

function loadHeader()
{
    include dirname(__FILE__) . '/header.php';
}

function loadFooter()
{
    include dirname(__FILE__) . '/footer.php';
}

function loadTemp($name, $loadHF = true)
{
    $filename = dirname(dirname(__FILE__)) . "/temples/" . $name;
    if (file_exists($filename)) {
        require_once $filename;
        return true;
    }
    return false;
}

function isAjaxRequest()
{
    global $_G;
    return $_G["ajax"];
}

/**
 * 读取POST 如果有则返回，没有则返回""
 *
 * @param string $name
 *            name
 */
function get_post($name)
{
    return isset($_POST[$name]) ? $_POST[$name] : "";
}

/**
 * 读取GET 如果有则返回，没有则返回""
 *
 * @param string $name
 *            name
 */
function get_get($name)
{
    return isset($_GET[$name]) ? $_GET[$name] : "";
}
?>
